package com.mentaleye.service.impl;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.PermissionsBean;
import com.mentaleye.dal.dao.PermissionsDao;
import com.mentaleye.dal.model.Permissions;
import com.mentaleye.dal.model.PermissionsExample;
import com.mentaleye.service.PermissionsService;
import com.mentaleye.util.CommonPage;

@Service
public class PermissionsServiceImpl implements PermissionsService{
	
	@Autowired private PermissionsDao permissionsDao;
	
	public PermissionsBean tansfer(Permissions permissions){
		PermissionsBean permissionsBean=new PermissionsBean();
		permissionsBean.setParentpermissionsId(permissions.getParentpermissionsId());
		permissionsBean.setPermissionsId(permissions.getPermissionsId());
		permissionsBean.setPermissionsName(permissions.getPermissionsName());
		permissionsBean.setPermissionsUrl(permissions.getPermissionsUrl());
		permissionsBean.setPermissionsType(permissions.getPermissionsType());
		permissionsBean.setPermissionsRoute(permissions.getPermissionsRoute());
		return permissionsBean;
	}
	
	public Permissions tansfer(PermissionsBean permissionsBean){
		Permissions permissions=new Permissions();
		permissions.setPermissionsId(permissionsBean.getPermissionsId());
		permissions.setPermissionsName(permissionsBean.getPermissionsName());
		permissions.setPermissionsType(permissionsBean.getPermissionsType());
		permissions.setPermissionsUrl(permissionsBean.getPermissionsUrl());
		permissions.setParentpermissionsId(permissionsBean.getParentpermissionsId());
		permissions.setPermissionsRoute(permissionsBean.getPermissionsRoute());
		return permissions;
	}
	private List<PermissionsBean> tansfer(List<Permissions> permissions){
		List<PermissionsBean> permissionsBeans=new ArrayList<PermissionsBean>();
		if(permissions==null)
			return null;
		for (Permissions permission : permissions) {
			permissionsBeans.add(tansfer(permission));
		}
		return permissionsBeans;
	}
	
		
	
	public boolean editPermissions(PermissionsBean permissionsBean,boolean add){
		boolean result=false;
		Permissions permissions=tansfer(permissionsBean);
		if(add){
			result=permissionsDao.insertPermissions(permissions);
		}else{
			result=permissionsDao.updatePermissions(permissions);
		}
		return result;
	}
	
	public boolean deletePermissions(List<Integer> permissionsId){
		return permissionsDao.deletePermissions(permissionsId);
	}
	
	public boolean deletePermissions(Integer parentpermissionsId){
		return permissionsDao.deletePermissionsByparentpermissionsId(parentpermissionsId);
	}

	@Override
	public void queryPermissions(CommonPage commonPage) {
		List<Permissions> data=permissionsDao.queryPermissions(commonPage.pageToMap());
		commonPage.setPageData(data);
		
	}

	@Override
	public List<Permissions> queryPermissions(PermissionsExample example) {
		
		return permissionsDao.queryPermissions(example);
	}
	public PermissionsBean queryPermissions(Integer permissionsId){
		return tansfer(permissionsDao.queryPermissions(permissionsId));
	}
	
	public List<PermissionsBean> queryPermissions(List<Integer> permissionsId){
		return tansfer(permissionsDao.queryPermissions(permissionsId));
	}
	
	public List<PermissionsBean> queryPermissions(int parentpermissionsId,String permissionsName){
		PermissionsExample example=new PermissionsExample();
		example.createCriteria().andParentpermissionsIdEqualTo(parentpermissionsId);
		example.createCriteria().andPermissionsNameEqualTo(permissionsName);
	return tansfer(permissionsDao.queryPermissions(example));
	
	}
	public PermissionsBean queryPermissionsByparentpermissionsId(Integer parentpermissionsId){
		return tansfer(permissionsDao.queryPermissionsByparentpermissionsId(parentpermissionsId));
	}
	

}
